package com.zhang.ziyan.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.ziyan.pojo.Backlog;
import com.zhang.ziyan.pojo.Issued;
import com.zhang.ziyan.pojo.Working;
import com.zhang.ziyan.service.BacklogService;
import com.zhang.ziyan.service.IssuedService;
import com.zhang.ziyan.service.WorkingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Api(tags = "发文管理")
@RestController
@RequestMapping("api/IssuedManage")
public class IssuedManageController {
        @Autowired
    IssuedService issuedService;
        @Autowired
    BacklogService backlogService;
        @Autowired
    WorkingService workingService;



    @ApiOperation("新建发文")
    @PostMapping("/add")
    public R add(Issued issued){

        issuedService.save(issued);
        Integer issuedid = issued.getIssuedid();
        Backlog backlog=new Backlog();
        backlog.setIssuedId(issuedid);
        backlog.setTitle(issued.getTitle());
        backlog.setProcess(issued.getModulename());
        backlog.setCreateTime( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        backlog.setStep("部门审核");
        backlog.setCreateDep(issued.getIssOpinionB());
        backlog.setHandler("管理员");
        backlogService.save(backlog);
        Working working=new Working();
        working.setIssuedId(issuedid);
        working.setTitle(issued.getTitle());
        working.setIssOpinionP(issued.getIssOpinionP());
        working.setCreateTime(backlog.getCreateTime());
        working.setStep(backlog.getStep());
        working.setCreateDep(backlog.getCreateDep());
        working.setHandler(backlog.getHandler());
        workingService.save(working);

        return R.ok(issuedid);
    }
    @ApiOperation("搜索待办")
    @PostMapping("/searchbacklog")
    public R lookissuedB(@RequestBody JSONObject jsonObject){






        LambdaQueryWrapper<Backlog> lambdaQueryWrapper =new LambdaQueryWrapper<>();

       lambdaQueryWrapper.eq(Backlog::getProcess,jsonObject.getString("process"))
               .eq(Backlog::getHandler,jsonObject.getString("handler"))
               .eq(Backlog::getState,jsonObject.getInteger("state"))
    .like(StringUtils.isNotBlank(jsonObject.getString("process")),Backlog::getProcess, jsonObject.get("process"))
                       .like(StringUtils.isNotBlank(jsonObject.getString("createdep")),Backlog::getCreateDep, jsonObject.get("createdep"))
                               .like(StringUtils.isNotBlank(jsonObject.getString("step")),Backlog::getStep, jsonObject.get("step"));

        Page<Backlog> page = backlogService.page(new Page<Backlog>(jsonObject.getLong("pageNo"), jsonObject.getLong("pageSize")), lambdaQueryWrapper);


        return R.ok(page);
    }
    @ApiOperation("搜索在办")
    @PostMapping("/searchworking")
    public R lookissuedW(@RequestBody JSONObject jsonObject){

        LambdaQueryWrapper<Working> lambdaQueryWrapper =new LambdaQueryWrapper<>();

        lambdaQueryWrapper
                .eq(Working::getState,jsonObject.getLong("state"))
                .like(StringUtils.isNotBlank(jsonObject.getString("title")),Working::getTitle, jsonObject.get("title"))
                .like(StringUtils.isNotBlank(jsonObject.getString("handler")),Working::getHandler, jsonObject.get("handler"));


        Page<Working> page = workingService.page(new Page<Working>(jsonObject.getLong("pageNo"), jsonObject.getLong("pageSize")), lambdaQueryWrapper);


        return R.ok(page);
    }

    @ApiOperation("查看文件明细")
    @PostMapping("/lookissued")
    public R lookissued(@RequestBody JSONObject jsonObject){

        LambdaQueryWrapper<Issued> lambdaQueryWrapper =new LambdaQueryWrapper<>();

        lambdaQueryWrapper
                .eq(Issued::getTitle, jsonObject.get("title"))
                .eq(Issued::getIssuedid, jsonObject.get("issuedId"));




        return R.ok(issuedService.getOne(lambdaQueryWrapper));
    }
    @ApiOperation("修改文件明细")
    @PostMapping("/updateissued")
    @Transactional
    public R updateissued(Issued issued,@RequestBody JSONObject jsonObject) throws RuntimeException {



        Issued nissued=new Issued();

        switch (issued.getHandlerflow()){
            case "部门审核":

                //加入是否上传栏
                nissued.setUploading(issued.getUploading());
                //意见栏
                nissued.setIssOpinion(issued.getIssOpinion());
               updateM(jsonObject.getLong("backlogid"), issued,"办公室主任审核","办公室领导");


                break;

            case "办公室主任审核":
                nissued.setFileHead(issued.getFileHead());
                nissued.setOffOpinion(issued.getOffOpinion());
                updateM(jsonObject.getLong("backlogid"), issued,"送印文室标号","办公室员工");

                break;
            case "文印室编号":
                nissued.setFileNumber(issued.getFileNumber());
                nissued.setNumber(issued.getNumber());
                updateM(jsonObject.getLong("backlogid"), issued,"机要室盖章分发","办公室员工");
                break;
            case "盖章分发":
                nissued.setEdit(issued.getEdit());
                Backlog backlog = new Backlog();
                backlog.setState(1);
                backlog.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                LambdaQueryWrapper<Backlog> lambdaQueryWrapper =new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(Backlog::getBacklogId,jsonObject.getLong("backlogid"))
                        .eq(Backlog::getState,0).eq(Backlog::getIssuedId,issued.getIssuedid());
               boolean b = backlogService.update(backlog, lambdaQueryWrapper);
                if (!b)
                {

                    throw new RuntimeException("未更新为已办完结");

                }

                Working working = new Working();
                working.setState(backlog.getState());
                working.setEndTime(backlog.getUpdateTime());
                Issued issued1 = issuedService.getById(issued.getIssuedid());
                working.setFileNumber(issued1.getFileHead()+issued1.getFileNumber());
                LambdaQueryWrapper<Working> lambdaQueryWrapper1 =new LambdaQueryWrapper<>();
                lambdaQueryWrapper1.eq(Working::getIssuedId,issued.getIssuedid())
                        .eq(Working::getState,0);
                b = workingService.update(working, lambdaQueryWrapper1);
                if (!b) {

                    throw new RuntimeException("未更新办结表");

                }


                break;

        }
        //更新文件表
        LambdaQueryWrapper<Issued> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Issued::getTitle, issued.getTitle())
                .eq(Issued::getIssuedid,issued.getIssuedid());
        return R.ok(issuedService.update(nissued,lambdaQueryWrapper));
    }




    public Boolean updateM(Long backlogid,Issued issued,String setStep,String setHandler) throws RuntimeException {

        Boolean b;

        Backlog backlog=new Backlog();

        //查记录对象添加变成已办
            Backlog backlogId = backlogService.getById(backlogid);
            backlogId.setState(1);
            backlogId.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            backlogService.save(backlogId);

            //更新待办表
            backlog.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            backlog.setStep(setStep);
            backlog.setHandler(setHandler);
            LambdaQueryWrapper<Backlog> lambdaQueryWrapper =new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Backlog::getBacklogId,backlogid)
                    .eq(Backlog::getState,0).eq(Backlog::getIssuedId,issued.getIssuedid());
            b = backlogService.update(backlog, lambdaQueryWrapper);
            if (!b)
            {

                throw new RuntimeException("未更新待办表");

            }
            //更新在办表
            Working working = new Working();
            working.setStep(backlog.getStep());
            working.setHandler(backlog.getHandler());
            LambdaQueryWrapper<Working> lambdaQueryWrapper1 =new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(Working::getIssuedId,issued.getIssuedid())
                    .eq(Working::getState,0);
            b = workingService.update(working, lambdaQueryWrapper1);
            if (!b) {

                throw new RuntimeException("未更新在办表");

            }

        return b;
    }

}
